<template>
  <div style="width:98%;margin: 0 auto;">
    <div style="width:100%;margin-top:20px;">
      <el-form :inline="true" size="medium">
        <el-form-item label="菜单名称：">
          <el-input
            v-model="queryParams.menuName"
            placeholder="请输入菜单名称"
            clearable
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="medium">
            <el-option label="正常" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getMenuList()">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="menuId"
        border
        size="medium"
        :default-expand-all="false"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          prop="menuName"
          label="菜单名称"
          header-align="center"
          width="200"
        >
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.icon" />
            <span>{{ scope.row.menuName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="orderNum"
          width="60"
          align="center"
          label="排序"
        />
        <el-table-column
          prop="componentPath"
          align="center"
          label="组件路径"
          show-overflow-tooltip
        />
        <el-table-column
          prop="routePath"
          align="center"
          show-overflow-tooltip
          label="路由地址"
        />
        <el-table-column
          prop="permissionIdentifier"
          align="center"
          label="权限标识"
          show-overflow-tooltip
        />
        <el-table-column
          prop="visible"
          align="center"
          width="100"
          label="导航栏显示"
        >
          <template slot-scope="scope">
            {{ scope.row.isOutside ?"是":"否" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="menuType"
          width="80"
          align="center"
          label="菜单类型"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.menuType === 'M'" effect="dark" size="medium" type="success">目录</el-tag>
            <el-tag v-if="scope.row.menuType === 'C'" effect="dark" size="medium" type="danger">菜单</el-tag>
            <el-tag v-if="scope.row.menuType === 'F'" effect="dark" size="medium" type="warning">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="isOutside"
          width="100"
          align="center"
          label="是否为外链"
        >
          <template slot-scope="scope">
            {{ scope.row.isOutside ?"是":"否" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" :content="scope.row.status === 1?'点击启用':'点击禁用'" placement="top">
              <el-button
                size="medium"
                :type="scope.row.status === 1?'warning':'primary'"
                plain
                @click="changeStatus(scope.row.menuId,scope.row.status === 1?0:1)"
              >{{ scope.row.status === 1?'禁用':'正常' }}</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          align="center"
          label="备注"
          show-overflow-tooltip
        />
        <el-table-column
          prop="address"
          align="center"
          width="200"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              size="medium"
              type="primary"
              @click="openEdit(scope.row.menuId)"
            >编辑</el-button>
            <el-button
              size="medium"
              type="danger"
              @click="deleteMenu(scope.row.menuId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <edit-dialog ref="editDialog" @refreshData="getMenuList" />
    </div>
  </div>
</template>

<script>
import EditDialog from './components/edit-dialog'
import { treeDataTranslate } from '@/utils'
export default {
  components: {
    EditDialog
  },
  data() {
    return {
      queryParams: {
        status: null,
        menuName: ''
      },
      tableData: []
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    /**
     * 获取菜单数据
     */
    getMenuList() {
      this.httpRequest({
        url: '/sys/menu/select',
        method: 'get',
        params: this.queryParams
      }).then((response) => {
        if (response.code === 0) {
          this.tableData = treeDataTranslate(response.body, 'menuId')
        } else {
          this.$message.error(response.msg)
        }
      })
    },
    /**
     * 删除菜单
     */
    deleteMenu(menuId) {
      this.$confirm('确认删除该菜单和该菜单的所有的子级菜吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.httpRequest({
          url: '/sys/menu/delete/' + menuId,
          method: 'post',
          data: {
            menuId: menuId,
            status: status
          }
        }).then((response) => {
          if (response.code === 0) {
            this.$message.success('删除成功')
            this.getMenuList()
          } else {
            this.$message.error(response.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    /**
     * 改变菜单状态
     */
    changeStatus(menuId, status) {
      let hint = ''
      let successHint = ''
      if (status === 0) {
        hint = '确认启用该菜单和该菜单的所有的上级菜吗？'
        successHint = '启用成功'
      } else {
        hint = '确认禁用该菜单和该菜单的所有的子级菜吗？'
        successHint = '禁用成功'
      }
      this.$confirm(hint, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.httpRequest({
          url: '/sys/menu/changeStatus',
          method: 'post',
          data: {
            menuId: menuId,
            status: status
          }
        }).then((response) => {
          if (response.code === 0) {
            this.$message.success(successHint)
            this.getMenuList()
          } else {
            this.$message.error(response.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    /**
     *打开编辑页
     */
    openEdit(id) {
      this.$refs.editDialog.init(id)
    },
    /**
     *打开编辑页
     */
    handleAdd() {
      this.$refs.editDialog.init()
    }
  }
}
</script>
